package com.example.controller;

import cn.hutool.core.date.DateUtil;
import com.example.common.Result;
import com.example.entity.HourlyWeather;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.zip.GZIPInputStream;

@RestController
@RequestMapping("/hourlyWeather")
public class HourlyWeatherController {

    private static final String BASE_URL = "https://mh33jk7rat.re.qweatherapi.com/v7/weather/24h?location=";
    private static final String API_KEY = "f02aa31a50c74b3186a25311b6862a72";

    @PostMapping("/get")
    public Map<String, Object> get(){
        int location = 101210106;
        String requestUrl = BASE_URL + location;
        HttpURLConnection connection = null;

        try {
            URL url = new URL(requestUrl);
            connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("X-QW-Api-Key", API_KEY);
            connection.setRequestProperty("Accept-Encoding", "gzip");

            connection.setConnectTimeout(60000);
            connection.setReadTimeout(60000);

            int responseCode = connection.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = connection.getInputStream();
                String encoding = connection.getContentEncoding();

                if ("gzip".equalsIgnoreCase(encoding)) {
                    inputStream = new GZIPInputStream(inputStream);
                }
                try (BufferedReader reader = new BufferedReader(
                        new InputStreamReader(inputStream, "UTF-8"))) {
                    StringBuilder responseBuilder = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        responseBuilder.append(line);
                    }
                    ObjectMapper objectMapper = new ObjectMapper();
                    Map<String, Object> jsonData = objectMapper.readValue(responseBuilder.toString(), Map.class);
                    return jsonData;
                }
            }
        } catch (Exception e) {
            return null;
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return null;
    }

    @PostMapping("/get/date")
    public Result getDate(){
        return Result.success(DateUtil.date());
    }
}
